package lt.ntec.danblog.server.blog.model.objectify;

import com.googlecode.objectify.Key;
import java.util.Date;
import java.util.List;
import java.util.Set;
import lt.ntec.danblog.server.blog.model.Post;


import javax.jdo.annotations.PersistenceCapable;
import javax.persistence.Id;
import javax.persistence.Transient;
import lt.ntec.danblog.server.core.model.Comment;
import lt.ntec.danblog.server.core.model.Tag;
import lt.ntec.danblog.server.core.model.User;

@PersistenceCapable
class BlogPost implements Post {

    private String title;
    private String content;
    private Date date;
    private Set<? extends Tag> tags;
    @Transient
    private User author;
    private Key<User> authorKey;
    private String preview;
    @Id
    private Long id;
    private int voteUp;
    private int voteDown;

    @Override
    public void setTitle(String title) {
        this.title = title;
    }

    @Override
    public void setContent(String content) {
        this.content = content;
    }

    @Override
    public void setDate(Date date) {
        this.date = date;
    }

    @Override
    public void setTags(Set<? extends Tag> tags) {
        this.tags = tags;
    }

    @Override
    public User getAuthor() {
        return author;
    }

    @Override
    public Date getDate() {
        return date;
    }

    @Override
    public String getContent() {
        return content;
    }

    @Override
    public String getTitle() {
        return title;
    }

    @Override
    public String getPreview() {
        return preview;
    }

    @Override
    public String getUrlString() {
        StringBuilder buff = new StringBuilder();
        for (Tag tag : getTags()) {
            buff.append(tag.getValue()).
                    append(" ");
        }
        if (buff.length() < 2) {
            return "";
        }
        return buff.substring(0, buff.length() - 1);
    }

    @Override
    public Set<? extends Tag> getTags() {
        return tags;
    }

    @Override
    public String getTagString() {
        StringBuilder buff = new StringBuilder();
        for (Tag tag : getTags()) {
            buff.append(tag.getValue()).
                    append(" ");
        }
        if (buff.length() < 2) {
            return "";
        }
        return buff.substring(0, buff.length() - 1);
    }

    @Override
    public Long getId() {
        return id;
    }

    @Override
    public void addComment(Comment comment) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<? extends Comment> getComments() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int getVoteUp() {
        return voteUp;
    }

    @Override
    public int getVoteDown() {
        return voteDown;
    }

    @Override
    public void addVoteDown() {
        voteDown++;
    }

    @Override
    public void addVoteUp() {
        voteUp++;
    }

    @Override
    public boolean hasUserVoted(User user) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
